package Data_Structure.List.MyList;

/**
 * @className: MyLinkedList2
 * @author: 芃
 * @description: 模拟实现双向链表
 * @date: 2023/8/22 18:44
 */
public class MyLinkedList2 {
    private ListNode2 head;//头节点
    private ListNode2 last;//尾节点

    //头插法
    public void addFirst(int val){
        ListNode2 node = new ListNode2(val);
        if (head == null){
            this.head = node;
            this.last = node;
        }else {
            node.next = this.head;
            this.head.prev = node;
            this.head = this.head.prev;
        }
    }

    //尾插法
    public void addEnd(int val){
        ListNode2 node = new ListNode2(val);
        if (last == null){
            this.head = node;
            this.last = node;
        }else {
            this.last.next = node;
            node.prev = this.last;
            this.last = this.last.next;
        }
    }

    //指定下标位置插入
    public boolean addIndex(int index,int val){
        int size = size();
        if (index < 0 || index > size){
            return false;
        }else {
            if (index == 0){//插入头
                addFirst(val);
            }else if (index == size){//插入尾
                addEnd(val);
            }else {//插入中间
                ListNode2 cur = this.head;
                //假设五个元素，插入到2位置，从0下标开始算，5-2-1 = 2
                //从零开始走，走两步走到2下标处，将node插入到2下标的前面
                while (index != 0){
                    cur = cur.next;
                    index--;
                }
                ListNode2 node = new ListNode2(val);
                ListNode2 prev = cur.prev;
                prev.next = node;
                node.prev = prev;
                node.next = cur;
                cur.prev = node;
            }
            return true;
        }
    }

    //查询链表中是否包含值为key的节点
    public boolean contains(int key){
        ListNode2 cur = this.head;
        while (cur != null){
            if (cur.val == key){
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    //删除第一个值为key的节点
    public void remove(int key){
        ListNode2 cur = this.head;
        while (cur != null){
            if (cur.val == key){
                //考虑头删和尾删
                if (cur == head){
                    this.head = this.head.next;
                    if (head != null){
                        this.head.prev = null;
                    }else {
                        last = null;
                    }
                }else if (cur == last){
                    this.last = this.last.prev;
                    this.last.next = null;
                }else {
                    ListNode2 prev = cur.prev;
                    ListNode2 next = cur.next;
                    prev.next = next;
                    next.prev = prev;
                }
                return;
            }else {
                cur = cur.next;
            }
        }
    }

    //删除所有值为key的节点
    public void removeAllKey(int key){
        ListNode2 cur = this.head;
        while (cur != null){
            if (cur.val == key){
                //考虑头删和尾删
                if (cur == head){
                    this.head = this.head.next;
                    if (head != null){
                        this.head.prev = null;
                    }else {
                        last = null;
                    }
                }else {
                    if (cur.prev != null){
                        cur.prev.next = cur.next;
                    }
                    if (cur.next != null){
                        cur.next.prev = cur.prev;
                    }else {
                        if (last != null) {
                            last = last.prev;
                        }
                    }
                }
            }else {
                cur = cur.next;
            }
        }
    }

    //获取链表的长度
    public int size(){
        int count = 0;
        ListNode2 cur = head;
        while (cur != null){
            count++;
            cur = cur.next;
        }
        return count;
    }

    //打印链表的元素
    public void display(){
        ListNode2 cur = this.head;
        while (cur != null){
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
    }

    //链表清空
    public void clear(){
        while (head != null){
            ListNode2 curNext = head.next;
            head.next = null;
            head.prev = null;
            head = curNext;
        }
        last = null;
    }
}
